<?php

namespace App\Http\Model;

use App\Http\Controllers\UploadController;
use Illuminate\Database\Eloquent\Model;

class LoginModel extends Model
{
    protected $table="admin";

    public function login($username,$password){

        $id = LoginModel::where("username",$username)->where("password",md5($password))->select("id")->first();

        if(strlen($id)>0){
            $uc = new UploadController();

            $token = md5($username.$uc->uuid());

            $admin = LoginTokenModel::where("admin_id",$id->id)->first();

            $ltm = empty($admin)?new LoginTokenModel():$admin;

            $ltm->admin_id = $id->id;

            $ltm->token = $token;

            $ltm->expire_time=strtotime("+1 day");

            $tokenId = $ltm->save();

            if($tokenId>0){
                return $token;
            }
            return false;
        }else{
            return false;
        }
    }
    //
}
